<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <jsp:include page="../inc/inc.jsp"></jsp:include>
        <script type="text/javascript">
            window.top["reload_data"]=function(){
                $('#datagd').datagrid("load");
                $.messager.show({
                    title: '提示',
                    msg: '保存成功'
                });
            };

            $(document).ready(function () {
                $("#datagd").datagrid({
                    url:'${pageContext.request.contextPath}/news_page_info',
                    striped: true,
                    border: false,
                    collapsible:false,		//是否可折叠的
                    loadMsg:'正在加载数据...',
                    fit: true,				//自动大小
                    idField:'id',
                    singleSelect:true,//是否单选
                    pagination:true,//分页控件
                    rownumbers:true,//行号
                    remoteSort:false,
                    columns:[[{
                        field:'title',
                        title:'标题',
                        sortable:true,
                        width:200,
                        halign:'center'
                    },{
                        field:'subTitle',
                        title:'小标题',
                        sortable:true,
                        width:200,
                        halign:'center'
                    },{
                        field:'time',
                        title:'时间',
                        sortable:true,
                        width:150,
                        halign:'center'
                    },{
                        field:'writer',
                        title:'作者',
                        width:300,
                        halign:'center'
                    }
                    ]],
                    toolbar: '#toolbar'
                });
            });
        </script>
    </head>

    <body>
        <div class="easyui-layout" data-options="fit:true,border:false" >
            <table id="datagd"></table>
        </div>

        <div id="toolbar" style="display:none">
            <a onclick="addNews();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">添加</a>
            <a onclick="editNews();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">修改</a>
            <a onclick="delNews();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'">删除</a>
        </div>
    </body>
</html>
<script type="text/javascript">
    //增加
    function addNews() {
//        var id = $("input[name='id']").val();
        self.parent.addTab("新闻编辑","news_edit","icon-3menu");
    }

    //修改
    function editNews(){
        var row = $('#datagd').datagrid('getSelected');
        if (row){
            self.parent.addTab("新闻编辑","news_edit?id="+row.id,"icon-3menu");
        } else {
            $.messager.alert("提示","请选择一条新闻");
        }
    }

    //删除
    function delNews(){
        var row = $('#datagd').datagrid('getSelected');
        if (row){
            $.messager.confirm('提示','请确认是否删除?',function(r){
                if (r){
                    $.ajax({
                        url:'news_del',
                        type:'POST',
                        data:{id:row.id},
                        success: function(data){
                            var result = eval('(' + data + ')');
                            if(result.success){
                                $('#datagd').datagrid('reload');
                                $('#datagd').datagrid('clearSelections');
                                $.messager.show({
                                    title: '提示',
                                    msg: result.msg
                                });
                            }else{
                                $.messager.show({
                                    title: '提示',
                                    msg: result.msg
                                });
                            }
                        }
                    });
                }
            });
        } else {
            $.messager.alert("提示","请选择一条新闻。");
        }
    }
</script>